/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.vacoor.ui.qq.password;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;
import javax.swing.plaf.TextUI;
import javax.swing.plaf.basic.BasicPasswordFieldUI;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

import org.vacoor.ui.qq.resources.UIResourceManager;

/**
 *
 * @author Vacoor
 */
public class QQPasswordField extends JPanel implements MouseListener {

	private static final long serialVersionUID = 1L;
	ScreenKeyboard scrKeyboard;
    JPasswordField passwordField;
    JButton buttonKey;
    private final Dimension size = new Dimension(180, 26);

    public QQPasswordField() {
        UIManager.put("PasswordField.echoChar", '●');
        scrKeyboard = new ScreenKeyboard();
        passwordField = new JPasswordField("");
//        buttonKey = new JButton(new ImageIcon(getClass().getResource("/ImageLoader/keyboard.png")));
        buttonKey = new JButton();
        buttonKey.setIcon(UIResourceManager.getMPassKeyboardImg());
        buttonKey.setPressedIcon(UIResourceManager.getMPassKeyboardPressImg());
        passwordField.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        passwordField.setUI(new BasicPasswordFieldUI() {
            @Override
            protected Highlighter createHighlighter() {
                return new DefaultHighlighter();
            }
        });
        buttonKey.setContentAreaFilled(false);
        buttonKey.setFocusPainted(false);
        buttonKey.setBorder(null);
        buttonKey.setBounds(2, 2, 20, 20);
        passwordField.addMouseListener(this);
        buttonKey.addMouseListener(this);
        this.setLayout(new BorderLayout());
        this.setBackground(Color.WHITE);
        this.setBorder(BorderFactory.createLineBorder(new Color(167, 195, 212)));
        this.add(passwordField, "Center");
        this.add(buttonKey, "East");
        this.setPreferredSize(size);
        this.setMinimumSize(size);
        this.setMaximumSize(size);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        setBorder(BorderFactory.createCompoundBorder(
                new javax.swing.border.LineBorder(new Color(96, 200, 253), 1, true),
                new javax.swing.border.LineBorder(new Color(51, 139, 192), 1, true)));
    }

    @Override
    public void mouseExited(MouseEvent e) {
        setBorder(BorderFactory.createLineBorder(new Color(167, 195, 212)));
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == buttonKey && javax.swing.SwingUtilities.isLeftMouseButton(e)) {
//            javax.swing.JOptionPane.showMessageDialog(passwordField, "键盘");
            if (scrKeyboard.isVisible()) {
                scrKeyboard.setVisible(false);
            } else {
                scrKeyboard.showScrKeyboard(buttonKey.getLocationOnScreen().x - 200, buttonKey.getLocationOnScreen().y + 25);
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }
}
